package com.eupmt.lab3.persistence.ibatis.dao;

import com.eupmt.lab3.domain.Rol;
import com.ibatis.common.resources.Resources;
import com.ibatis.dao.client.DaoManager;
import com.ibatis.dao.client.DaoManagerBuilder;
import java.io.IOException;
import java.io.Reader;
import java.util.List;

/**
 *
 * @author sony
 */
public class RolDaoManager {

    protected static RolDaoManager instance = null;
    private RolDAO rolDAO = null;

    private RolDaoManager() throws IOException {
        super();
        Reader readerDao = Resources.getResourceAsReader("com/eupmt/lab3/persistence/ibatis/daoConfig.xml");
        DaoManager daoManager = DaoManagerBuilder.buildDaoManager(readerDao);
        this.rolDAO = (RolDAO) daoManager.getDao(RolDAO.class);
//        rolDAO = new RolDAOImpl();
    }

    //Mètodes de Rol
    public void saveRol(Rol rol) throws Exception {
        rolDAO.saveRol(rol);
    }

    public void insertRol(Rol rol) throws Exception {
        rolDAO.insertRol(rol);
    }

    public void updateRol(Rol rol) throws Exception {
        rolDAO.updateRol(rol);
    }

    public void deleteRol(Rol rol) throws Exception {
        rolDAO.deleteRol(rol);
    }

    public Rol getRolById(int idRol) throws Exception{
        return rolDAO.getRolById(idRol);
    }

    public Rol getRolByNom(String nom) throws Exception {
        return rolDAO.getRolByNom(nom);
    }

    public List<Rol> getAllRols() throws Exception {
        return rolDAO.getAllRols();
    }

    public List<Rol> getRolsLikeNom(String nom) throws Exception {
        return rolDAO.getRolsLikeNom(nom);
    }

    public static RolDaoManager getInstance() throws IOException {
        if (instance == null) {
            instance = new RolDaoManager();
        }
        return instance;
    }
}
